package com.tpl.core.basedata.action;


import com.tpl.core.JsonUtil;
import com.tpl.core.action.BaseAction;
import com.tpl.core.action.SessionUtil;
import com.tpl.core.basedata.service.AuthService;
import com.tpl.core.common.redis.RedisClientUtil;
import com.tpl.core.entity.SessionUser;
import com.tpl.core.entity.sys.User;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * 说明 :登录操作Action
 * @author <a href="mailto:3380084@gmail.com">张坤雷</a>
 * @version 创建时间：2013-7-2 下午5:46:18 
 *
 */
@Controller
public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2575264620880575312L;
	private Log log= LogFactory.getLog(LoginAction.class);
	@Autowired
	private AuthService authService;
//	@Autowired
//	private RedisClientTemplate redisClientTemplate;
	@RequestMapping("/login.do")
	public @ResponseBody
    void login(User user){
 		SessionUser suser=authService.doLogin(user);
		//已经登录过用户，并且跟当前用户不同
		SessionUtil.setLoginUser(suser);
		authService.removeLock(suser.getUserNo(), null);
		String token=UUID.randomUUID().toString();
		RedisClientUtil.set(token, suser,60*30);
		JSONObject jo= JsonUtil.toJSONObject(suser);
		outJsonSuccess(jo.toString());
	}
	
	@RequestMapping("/loginOut.do")
	public
    String loginOut() throws Exception{
		if(getRequest().getSession(false)!=null){
			getRequest().getSession().invalidate();
		}
		return "index";
	}

	@RequestMapping("/testBody.do")
	public void testBody(String userName,String userNo) throws Exception{
		outJsonSuccess(userName+"==="+userNo);

	}


}


